<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="客户名" prop="username">
        <el-input v-model="queryParams.username" placeholder="请输入用户名" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="电话号码" prop="phone">
        <el-input v-model="queryParams.phone" placeholder="请输入联系电话" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <!-- <el-form-item label="预约时间" prop="appointTime">
        <el-date-picker clearable v-model="queryParams.appointTime" type="date" value-format="yyyy-MM-dd"
          placeholder="请选择预约时间">
        </el-date-picker>
      </el-form-item> -->
      <!-- <el-form-item label="到店时间" prop="arrivalTima">
        <el-date-picker clearable v-model="queryParams.arrivalTima" type="date" value-format="yyyy-MM-dd"
          placeholder="请选择到店时间">
        </el-date-picker>
      </el-form-item> -->
      <el-form-item label="车牌号码" prop="licensePlate">
        <el-input v-model="queryParams.licensePlate" placeholder="请输入车牌号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <!-- <el-form-item label="备注" prop="info">
        <el-input v-model="queryParams.info" placeholder="请输入备注" clearable @keyup.enter.native="handleQuery" />
      </el-form-item> -->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['appointment:info:add']">新增</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['appointment:info:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['appointment:info:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['appointment:info:export']">导出</el-button>
      </el-col> -->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="infoList" @selection-change="handleSelectionChange">
      <!-- <el-table-column type="selection" width="55" align="center" /> -->
      <!-- <el-table-column label="id主键唯一标识" align="center" prop="id" /> -->
      <el-table-column label="客户名" align="center" prop="username" />
      <el-table-column label="联系电话" align="center" prop="phone" />
      <el-table-column label="预约时间" align="center" prop="appointTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.appointTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="到店时间" align="center" prop="arrivalTima" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.arrivalTima, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="车牌号" align="center" prop="licensePlate" />
      <el-table-column label="汽车类型" align="center" prop="carType" />
      <el-table-column label="服务类型" align="center" prop="serviceType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.bus_appointment_service" :value="scope.row.serviceType" />
        </template>
      </el-table-column>

      <el-table-column label="备注" align="center" prop="info" />
      <el-table-column label="状态 " align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.bus_appointment_status" :value="scope.row.status" />
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="150">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['appointment:info:edit']" v-if="scope.row.status === 0">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-goods" @click="handleArrival(scope.row)"
            v-hasPermi="['appyointment:info:edit']" v-if="scope.row.status === 0">到店</el-button>
          <el-button size="mini" type="text" icon="el-icon-s-order" @click="doCreateStatement(scope.row)"
            v-hasPermi="['appointment:info:edit']"
            v-if="scope.row.status === 1 || scope.row.status === 4 || scope.row.status === 5">结算单</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleCancel(scope.row)"
            v-hasPermi="['appointment:info:remove']" v-if="scope.row.status === 0">取消</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改预约
对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="客户名" prop="username">
          <el-input v-model="form.username" placeholder="请输入用户名" />
        </el-form-item>
        <el-form-item label="客户联系方式" prop="phone">
          <el-input v-model="form.phone" placeholder="请输入联系电话" />
        </el-form-item>
        <el-form-item label="预约时间" prop="appointTime">
          <!-- <el-date-picker clearable v-model="form.appointTime" type="date" value-format="yyyy-MM-dd"
            placeholder="请选择预约时间">
          </el-date-picker> -->
          <el-date-picker clearable v-model="form.appointTime" type="datetime" value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="请选择预约时间" align="right">
          </el-date-picker>
        </el-form-item>
        <!-- <el-form-item label="到店时间" prop="arrivalTima">
          <el-date-picker clearable v-model="form.arrivalTima" type="date" value-format="yyyy-MM-dd"
            placeholder="请选择到店时间">
          </el-date-picker>
        </el-form-item> -->
        <el-form-item label="车牌号码" prop="licensePlate">
          <el-input v-model="form.licensePlate" placeholder="请输入车牌号" />
        </el-form-item>
        <el-form-item label="汽车类型" prop="carType">
          <el-input v-model="form.carType" placeholder="请输入汽车类型" />
        </el-form-item>
        <el-form-item label="服务类型" prop="serviceType">
          <el-select clearable v-model="form.serviceType" placeholder="请选择">
            <el-option v-for="item in dict.type.bus_appointment_service" :key="item.value" :label="item.label"
              :value="parseInt(item.value)">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="备注信息" prop="info">
          <el-input v-model="form.info" placeholder="请输入备注信息" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listInfo, getInfo, delInfo, addInfo, updateInfo, arrivalInfo, cancelInfo,createStatement } from "@/api/appointment/info";
import { validphone, validlicenseplate } from "@/utils/validate";

export default {
  name: "Info",
  dicts: [
    'bus_appointment_service',
    'bus_appointment_status'
  ],
  data() {
    let validatephone = (rule, value, callback) => {
      if (!validphone(value)) {
        callback(new Error('号码格式有误'));
      } else {
        callback();
      }
    };
    let validatelicenseplate = (rule, value, callback) => {
      if (!validlicenseplate(value)) {
        callback(new Error('车牌号格式有误'));
      } else {
        callback();
      }
    };
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 预约
      // 表格数据
      infoList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        username: null,
        phone: null,
        appointTime: null,
        arrivalTima: null,
        licensePlate: null,
        carType: null,
        serviceType: null,
        info: null,
        status: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        username: [
          { required: true, message: "用户名不能为空", trigger: "blur" },
          { min: 1, max: 9, message: '长度在 1 到 5 个字符', trigger: 'blur' }
        ],
        phone: [
          { required: true, message: "联系电话不能为空", trigger: "blur" },
          { validator: validatephone, trigger: "blur" }
        ],
        appointTime: [
          { required: true, message: '预约时间不能为空', trigger: 'change' }
        ],
        licensePlate: [
          { required: true, message: '车牌号不能为空', trigger: 'blur' },
          { validator: validatelicenseplate, trigger: "blur" }
        ],
        carType: [
          { required: true, message: '汽车类型不能为空', trigger: 'blur' }
        ],
        serviceType: [
          { required: true, message: '服务类型不能为空', trigger: 'change' }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询预约
列表 */
    getList() {
      this.loading = true;
      listInfo(this.queryParams).then(response => {
        this.infoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        username: null,
        phone: null,
        appointTime: null,
        arrivalTima: null,
        licensePlate: null,
        carType: null,
        serviceType: null,
        createTime: null,
        info: null,
        status: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加预约";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getInfo(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改预约  ";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateInfo(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addInfo(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /**已到店按钮操作 */
    handleArrival(row) {
      this.$modal.confirm('是否确认用户已到店').then(function () {
        return arrivalInfo(row.id);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("客户已到店");
      }).catch(() => { });
    },
    /** 取消按钮操作 */
    handleCancel(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认取消预约').then(function () {
        return cancelInfo(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("取消预约成功");
      }).catch(() => { });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除预约编号为"' + ids + '"的数据项？').then(function () {
        return delInfo(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('appointment/info/export', {
        ...this.queryParams
      }, `info_${new Date().getTime()}.xlsx`)
    },
    //创建结算单
    doCreateStatement(row) {
      createStatement(row.id).then(response => {
        let statement = response.data;
        let url = `/appointment/statement/item/${statement.id}`;
        if (statement.status === 1) {
          url = `/appointment/statement/details/${statement.id}`;
        }
        this.$router.push(url);
      })
    }
  }
};
</script>
